//
//  NBADataManager+Player.swift
//  NBA
//
//  Created by tiger on 2018/2/3.
//  Copyright © 2018年 Meigabay. All rights reserved.
//

import UIKit
import SQLite

internal class NBAClubBossTableColumn: NBAWorkerTableColumn {
    //固定信息
    let clubId = Expression<String>("clubId")
}

extension NBADBManager {
    
    internal func createClubBossTable() throws {
        let playerColumns = NBAClubBossTableColumn()
        let createDownloadVideoTable = self.nbaBossTable.create(ifNotExists: true) { (table) in
            table.column(playerColumns.workerId,primaryKey: true)
            table.column(playerColumns.avatar)
            table.column(playerColumns.name)
            table.column(playerColumns.nickname)
            table.column(playerColumns.gender)
            table.column(playerColumns.height)
            table.column(playerColumns.weight)
            table.column(playerColumns.birthDay)
            table.column(playerColumns.drafted)
            table.column(playerColumns.introduction)
            table.column(playerColumns.createTime)
            table.column(playerColumns.type)
            table.column(playerColumns.clubId)
        }
        do {
            try self.db.run(createDownloadVideoTable)
            LogUtil.i("created boss table")
        } catch {
            LogUtil.e(error)
            throw(error)
        }
    }
    
    internal func insertClubBoss(boss: NBAClubBoss) throws {
        let columns = NBAClubBossTableColumn()
        let insertBoss = self.nbaBossTable.insert(columns.workerId <- boss.workerId,
                                                  columns.clubId <- boss.clubId,
                                                  columns.avatar <- boss.avatar,
                                                  columns.name <- boss.name,
                                                  columns.nickname <- boss.nickname,
                                                  columns.gender <- boss.gender.rawValue,
                                                  columns.height <- boss.height,
                                                  columns.weight <- boss.weight,
                                                  columns.birthDay <- boss.birthDay,
                                                  columns.drafted <- boss.drafted,
                                                  columns.introduction <- boss.introduction,
                                                  columns.createTime <- Date().timeIntervalSince1970,
                                                  columns.type <- boss.type.rawValue)
        do {
            try self.db.run(insertBoss)
            LogUtil.i("Insert Boss Success")
        } catch {
            LogUtil.e(error)
            throw(error)
        }
    }
    
    /// 获取老板
    ///
    /// - Returns: 老板
    func filterBoss(clubId: String) -> NBAClubBoss? {
        let bossColumns = NBAClubBossTableColumn()
        let filterWorker = self.nbaWorkerTable.filter(bossColumns.clubId == clubId)
        do {
            let results = try self.db.prepare(filterWorker)
            for result in results {
                let boss = NBAClubBoss(result: result)
                return boss
            }
            
        } catch {
            LogUtil.i(error)
            return nil
        }
        return nil
    }
    
}

